LD=g++
LDFLAGS=
LIBS=-lrt -lpthread `xml2-config --libs` `wx-config --libs std,gl --unicode=no` -lusb \
	$(LIBSGIMI)
GDIR=../../..
SRCSC=
SRCSCPP=
GIMIPATH=$(GDIR)/GIMI
MACIPATH=$(GDIR)/MaCI
GIMBOPATH=$(GDIR)/Gimbo
VPATH=
CPPGIMI=
CGIMI=

C=gcc
CXX=g++
CFLAGS=-Wall $(DEBUG) $(DEFINITIONS) $(CINCLUDES) $(CMISCFLAGS) $(CE)
CPPFLAGS=$(DEBUG) $(CPPINCLUDES) $(DEFINITIONS) $(CPPMISCFLAGS) $(CE)
DCPPFLAGS=$(DEBUG) $(CPPINCLUDES) $(DEFINITIONS) $(CE)
DEBUG=-O0 -g3
CMISCFLAGS=
CPPMISCFLAGS=-Wall -Wunused -Wshadow -Weffc++
DEFINITIONS=-DLINUX_OS -DGIMI_NO_DATATYPE_INCLUDES $(CE)
CINCLUDES=-I. -I../../ -I$(GDIR)/include/GIMnetAP -I$(GDIR)/utils
CPPINCLUDES=-I. -I../../ \
		-I$(MACIPATH)/launcher \
		-I$(MACIPATH)/bus/USB \
		`xml2-config --cflags` \
		`wx-config --cflags --unicode=no` \
		$(MACI_INCLUDE_PATHS) \
	        $(INCGIMI)

VPATH=
CPPGIMI=
CGIMI=

## Build GIMI and it's dependencies.
## Files are set to CGIMI and CPPGIMI variables
include $(GDIR)/GIMI/Makefile.gimi

## Include MaCI-stuff
include $(MACIPATH)/interfaces/MaCI-interfaces.Makefile

## Define GIMBO sources
GIMBOSRCSC=
GIMBOSRCSCPP=$(GIMBOPATH)/Component.cpp

## Construct final lists of CPP and C objects
OBJSC=$(CGIMI:.c=.o) $(GIMBOSRCSC:.c=.o)
OBJSCPP=$(CPPGIMI:.cpp=.opp) \
	$(MACI_BASE_SRC_CPP:.cpp=.opp) \
	$(MACI_EXT_SRC_CPP:.cpp=.opp) \
	$(MACI_INTERFACE_EMERGENCYSTOP_COMMON_CPP:.cpp=.opp) \
	$(MACI_INTERFACE_EMERGENCYSTOPCLIENT_CPP:.cpp=.opp) \
	$(GIMBOSRCSCPP:.cpp=.dopp)

## Define compiles and links
COMPILEC=$(CC) $(CFLAGS) -c
COMPILECPP=$(CXX) $(CPPFLAGS) -c
COMPILEDCPP=$(CXX) $(DCPPFLAGS) -c
LINK=$(LD) $(LDFLAGS) $(LIBS)

.PHONY: all clean

TARGETS=MaCI_EmergencyStop KeyWatcherMain USBPanicButton Permissions

all: $(TARGETS) 

.PHONY: all clean Permissions

KeyWatcherMain:$(OBJSC) $(OBJSCPP) \
	KeyWatcherMain.opp \
	KeyWatcher.opp
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

USBPanicButton:$(OBJSC) $(OBJSCPP) \
	USBPanicButtonMain.opp \
	$(MACIPATH)/bus/USB/USB_Linux.opp \
	USBPanicButton.opp
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

Permissions:
	@if [ ! `id -u` -eq 0 ]; then echo "WARNING:You are not root. You Can't use keyboard or USB Panic button as an emergency stop"; \
	else chown root KeyWatcherMain; chmod 4711 KeyWatcherMain; chown root USBPanicButton; chmod 4711 USBPanicButton; fi


MaCI_EmergencyStop: $(OBJSC) $(OBJSCPP) \
	MaCI_EmergencyStop.dopp \
	MaCI_EmergencyStop_GUI.dopp \
	MaCI_EmergencyStopMainFrame.dopp \
	MaCI_EmergencyStopDeactivationDialog.dopp \
	MaCI_EmergencyStopAbout_Dialog.dopp \
	MaCI_EmergencyStopInformation_Dialog.dopp \
	MaCI_EmergencyStopHelp_Dialog.dopp \
	$(MACIPATH)/launcher/executor.opp 
	@$(LINK) -o $@ $^
	@echo "      [LD] $@"

clean:
	@rm -f *.dopp *.opp *.o *~ $(TARGETS)
	@echo "      [CLEAN]"

realclean: clean
	@rm -f *.state *.log $(OBJSC) $(OBJSCPP)
	@echo "      [REALCLEAN]"


## Below this is dependency generation stuff, no need to change.
## Compile rule for all .o (.c) file
%.o:    %.c
	@$(COMPILEC) -o $@ $<
	@echo "      [CC] $<"

## Compile rule for all .opp (.cpp) files
%.opp:  %.cpp
	@$(COMPILECPP) -o $@ $<
	@echo "      [CXX] $<"

# Compile rule for all .dopp (dirty .cpp) files
%.dopp:	%.cpp
	@$(COMPILEDCPP) -o $@ $<
	@echo "      [DIRTY-CXX] $<"

